/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import javax.swing.JToggleButton;
import game.server.IGameServer;
import java.awt.Color;
import java.awt.Component;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

/**
 *
 * @author RERIR
 */
public class GameField extends javax.swing.JPanel {
    public final static int SIZE_FIELD = 5;
    
    private String sideIcon;
    private String anotherSideIcon;
    private MoveListener action;
    private int lastOwnerMove;
    private int lastAhotherSideMove;
    private boolean ownerRound;
    private boolean canUndo;
    private JToggleButton[] control = new JToggleButton[SIZE_FIELD*SIZE_FIELD];

    /**
     * Создать игровое поле
     * @param side - сторона, которой поле показыватеся. IGameServer.PLUS - за крестики, IGameServer.ZERO - за нолики
     * @param moveAction - объект реализующий интерфейс MoveListener, используется для обратного вызова, когда хозяин поля сделает ход.
     */
    
    public GameField(int side, MoveListener moveAction) {
        
        if (side == IGameServer.PLUS) {
            this.sideIcon = "X";
            this.anotherSideIcon = "O";
            ownerRound = true;
        }
        else {
            this.sideIcon = "O";
            this.anotherSideIcon = "X";
            ownerRound = false;
        }
        if (moveAction != null) action = moveAction;
        else action = new MoveListener() {

            @Override
            public void MovePerformed(int ae) {
                // do nothing
            }
        };
        canUndo = false;
        lastAhotherSideMove = -1;
        lastOwnerMove = -1;
        
        initComponents();
        synchronized (this.getTreeLock()) {
            Component[] panelContent = this.getComponents();
            for (int i = 0; i < SIZE_FIELD*SIZE_FIELD; i++) {
                if (panelContent[i] instanceof JToggleButton) {
                    try {
                        int indx = Integer.parseInt(panelContent[i].getName())-1;
                        control[indx]=(JToggleButton)panelContent[i];
                    } catch (NumberFormatException e) {
                        assert false : "Ошибка в названии ToggleButton";
                    } catch (ArrayIndexOutOfBoundsException e) {
                        assert false : "Ошибка в названии ToggleButton";
                    }   
                }
            }
        }
    }
    
    /**
     * Показать ход другой строны
     * @param pos - позиция хода
     */
    
    public void moveAnotherSide (int pos) {
        if (pos <0 || pos >= SIZE_FIELD*SIZE_FIELD || control[pos] == null) return;
        if (!control[pos].isSelected()) {
            control[pos].setText(anotherSideIcon);
            control[pos].setSelected(true);
            control[pos].setEnabled(false);
            lastAhotherSideMove = pos;
            canUndo = true;
            ownerRound = true;
        }
    }
    
    /**
     * Отменить последний ход
     */
    public void undoLastMove() {
        if (canUndo) {
            if (ownerRound) {
                if (lastAhotherSideMove >= 0) {
                    clearMove(lastAhotherSideMove);
                    lastAhotherSideMove = -1;                    
                }
                canUndo = false;
                ownerRound = false;
            }
            else {
                if (lastOwnerMove >= 0) {
                    clearMove(lastOwnerMove);
                    lastOwnerMove = -1;
                    };
                canUndo = false;
                ownerRound = true;
            }
        }
    }
    
    /**
     * Подсветить выигрышный путь
     * @param path - строка, преставляющая путь, в позициях клеток, которые нужно подсветить - не пробел
     */
    
    public void highlightPath(String path) {
        
        char[] c = path.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] != '\b') highlightCell(i);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jToggleButton3 = new javax.swing.JToggleButton();
        jToggleButton4 = new javax.swing.JToggleButton();
        jToggleButton5 = new javax.swing.JToggleButton();
        jToggleButton6 = new javax.swing.JToggleButton();
        jToggleButton7 = new javax.swing.JToggleButton();
        jToggleButton8 = new javax.swing.JToggleButton();
        jToggleButton9 = new javax.swing.JToggleButton();
        jToggleButton10 = new javax.swing.JToggleButton();
        jToggleButton11 = new javax.swing.JToggleButton();
        jToggleButton12 = new javax.swing.JToggleButton();
        jToggleButton13 = new javax.swing.JToggleButton();
        jToggleButton14 = new javax.swing.JToggleButton();
        jToggleButton15 = new javax.swing.JToggleButton();
        jToggleButton16 = new javax.swing.JToggleButton();
        jToggleButton17 = new javax.swing.JToggleButton();
        jToggleButton18 = new javax.swing.JToggleButton();
        jToggleButton19 = new javax.swing.JToggleButton();
        jToggleButton20 = new javax.swing.JToggleButton();
        jToggleButton21 = new javax.swing.JToggleButton();
        jToggleButton22 = new javax.swing.JToggleButton();
        jToggleButton23 = new javax.swing.JToggleButton();
        jToggleButton24 = new javax.swing.JToggleButton();
        jToggleButton25 = new javax.swing.JToggleButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setMaximumSize(new java.awt.Dimension(525, 525));
        setMinimumSize(new java.awt.Dimension(275, 275));
        setPreferredSize(new java.awt.Dimension(375, 375));
        setLayout(new java.awt.GridLayout(5, 5, 5, 5));

        jToggleButton1.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton1.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton1.setFocusPainted(false);
        jToggleButton1.setFocusable(false);
        jToggleButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton1.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton1.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton1.setName("1"); // NOI18N
        jToggleButton1.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton1);

        jToggleButton2.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton2.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton2.setFocusPainted(false);
        jToggleButton2.setFocusable(false);
        jToggleButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton2.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton2.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton2.setName("2"); // NOI18N
        jToggleButton2.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton2);

        jToggleButton3.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton3.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton3.setFocusPainted(false);
        jToggleButton3.setFocusable(false);
        jToggleButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton3.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton3.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton3.setName("3"); // NOI18N
        jToggleButton3.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton3);

        jToggleButton4.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton4.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton4.setFocusPainted(false);
        jToggleButton4.setFocusable(false);
        jToggleButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton4.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton4.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton4.setName("4"); // NOI18N
        jToggleButton4.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton4);

        jToggleButton5.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton5.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton5.setFocusPainted(false);
        jToggleButton5.setFocusable(false);
        jToggleButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton5.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton5.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton5.setName("5"); // NOI18N
        jToggleButton5.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton5);

        jToggleButton6.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton6.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton6.setFocusPainted(false);
        jToggleButton6.setFocusable(false);
        jToggleButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton6.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton6.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton6.setName("6"); // NOI18N
        jToggleButton6.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton6);

        jToggleButton7.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton7.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton7.setFocusPainted(false);
        jToggleButton7.setFocusable(false);
        jToggleButton7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton7.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton7.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton7.setName("7"); // NOI18N
        jToggleButton7.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton7);

        jToggleButton8.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton8.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton8.setFocusPainted(false);
        jToggleButton8.setFocusable(false);
        jToggleButton8.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton8.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton8.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton8.setName("8"); // NOI18N
        jToggleButton8.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton8);

        jToggleButton9.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton9.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton9.setFocusPainted(false);
        jToggleButton9.setFocusable(false);
        jToggleButton9.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton9.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton9.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton9.setName("9"); // NOI18N
        jToggleButton9.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton9);

        jToggleButton10.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton10.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton10.setFocusPainted(false);
        jToggleButton10.setFocusable(false);
        jToggleButton10.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton10.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton10.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton10.setName("10"); // NOI18N
        jToggleButton10.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton10);

        jToggleButton11.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton11.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton11.setFocusPainted(false);
        jToggleButton11.setFocusable(false);
        jToggleButton11.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton11.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton11.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton11.setName("11"); // NOI18N
        jToggleButton11.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton11);

        jToggleButton12.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton12.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton12.setFocusPainted(false);
        jToggleButton12.setFocusable(false);
        jToggleButton12.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton12.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton12.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton12.setName("12"); // NOI18N
        jToggleButton12.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton12);

        jToggleButton13.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton13.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton13.setFocusPainted(false);
        jToggleButton13.setFocusable(false);
        jToggleButton13.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton13.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton13.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton13.setName("13"); // NOI18N
        jToggleButton13.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton13);

        jToggleButton14.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton14.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton14.setFocusPainted(false);
        jToggleButton14.setFocusable(false);
        jToggleButton14.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton14.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton14.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton14.setName("14"); // NOI18N
        jToggleButton14.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton14);

        jToggleButton15.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton15.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton15.setFocusPainted(false);
        jToggleButton15.setFocusable(false);
        jToggleButton15.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton15.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton15.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton15.setName("15"); // NOI18N
        jToggleButton15.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton15);

        jToggleButton16.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton16.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton16.setFocusPainted(false);
        jToggleButton16.setFocusable(false);
        jToggleButton16.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton16.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton16.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton16.setName("16"); // NOI18N
        jToggleButton16.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton16);

        jToggleButton17.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton17.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton17.setFocusPainted(false);
        jToggleButton17.setFocusable(false);
        jToggleButton17.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton17.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton17.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton17.setName("17"); // NOI18N
        jToggleButton17.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton17);

        jToggleButton18.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton18.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton18.setFocusPainted(false);
        jToggleButton18.setFocusable(false);
        jToggleButton18.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton18.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton18.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton18.setName("18"); // NOI18N
        jToggleButton18.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton18.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton18);

        jToggleButton19.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton19.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton19.setFocusPainted(false);
        jToggleButton19.setFocusable(false);
        jToggleButton19.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton19.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton19.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton19.setName("19"); // NOI18N
        jToggleButton19.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton19.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton19);

        jToggleButton20.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton20.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton20.setFocusPainted(false);
        jToggleButton20.setFocusable(false);
        jToggleButton20.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton20.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton20.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton20.setName("20"); // NOI18N
        jToggleButton20.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton20.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton20);

        jToggleButton21.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton21.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton21.setFocusPainted(false);
        jToggleButton21.setFocusable(false);
        jToggleButton21.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton21.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton21.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton21.setName("21"); // NOI18N
        jToggleButton21.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton21.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton21);

        jToggleButton22.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton22.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton22.setFocusPainted(false);
        jToggleButton22.setFocusable(false);
        jToggleButton22.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton22.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton22.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton22.setName("22"); // NOI18N
        jToggleButton22.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton22.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton22);

        jToggleButton23.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton23.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton23.setFocusPainted(false);
        jToggleButton23.setFocusable(false);
        jToggleButton23.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton23.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton23.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton23.setName("23"); // NOI18N
        jToggleButton23.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton23.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton23);

        jToggleButton24.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton24.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton24.setFocusPainted(false);
        jToggleButton24.setFocusable(false);
        jToggleButton24.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton24.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton24.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton24.setName("24"); // NOI18N
        jToggleButton24.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton24.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton24);

        jToggleButton25.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
        jToggleButton25.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        jToggleButton25.setFocusPainted(false);
        jToggleButton25.setFocusable(false);
        jToggleButton25.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton25.setMaximumSize(new java.awt.Dimension(100, 100));
        jToggleButton25.setMinimumSize(new java.awt.Dimension(50, 50));
        jToggleButton25.setName("25"); // NOI18N
        jToggleButton25.setPreferredSize(new java.awt.Dimension(70, 70));
        jToggleButton25.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton25ActionPerformed(evt);
            }
        });
        add(jToggleButton25);
    }// </editor-fold>//GEN-END:initComponents

    private void jToggleButton25ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton25ActionPerformed
        JToggleButton but = (JToggleButton)evt.getSource();
        if (!ownerRound) {
            but.setSelected(false);
            return;
        }
        int move;
        try {
            move = Integer.parseInt(but.getName())-1;
        } catch (NumberFormatException e) {
            return;
        }
        but.setText(sideIcon);
        but.setEnabled(false);
        lastOwnerMove = move;
        canUndo = true;
        ownerRound = false;
        action.MovePerformed(move);
    }//GEN-LAST:event_jToggleButton25ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton10;
    private javax.swing.JToggleButton jToggleButton11;
    private javax.swing.JToggleButton jToggleButton12;
    private javax.swing.JToggleButton jToggleButton13;
    private javax.swing.JToggleButton jToggleButton14;
    private javax.swing.JToggleButton jToggleButton15;
    private javax.swing.JToggleButton jToggleButton16;
    private javax.swing.JToggleButton jToggleButton17;
    private javax.swing.JToggleButton jToggleButton18;
    private javax.swing.JToggleButton jToggleButton19;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton20;
    private javax.swing.JToggleButton jToggleButton21;
    private javax.swing.JToggleButton jToggleButton22;
    private javax.swing.JToggleButton jToggleButton23;
    private javax.swing.JToggleButton jToggleButton24;
    private javax.swing.JToggleButton jToggleButton25;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToggleButton jToggleButton4;
    private javax.swing.JToggleButton jToggleButton5;
    private javax.swing.JToggleButton jToggleButton6;
    private javax.swing.JToggleButton jToggleButton7;
    private javax.swing.JToggleButton jToggleButton8;
    private javax.swing.JToggleButton jToggleButton9;
    // End of variables declaration//GEN-END:variables

    private void clearMove(int lastMove) {
        control[lastMove].setSelected(false);
        control[lastMove].setEnabled(true);
        control[lastMove].setText("");
    }

    private void highlightCell(int i) {
        if (i <0 || i >= SIZE_FIELD*SIZE_FIELD || control[i] == null) return;
        Color fc = control[i].getForeground();
        Color bc = control[i].getBackground();
        control[i].setForeground(bc);
        control[i].setBackground(fc);
        
//        Border border = new BevelBorder(BevelBorder.RAISED, Color.GREEN,Color.DARK_GRAY);
//        control[i].setBorder(border);
    }
}
